<%@ page import="com.eyas.business.model.jpa.AdminInfo" %>
<%@ page import="com.eyas.business.config.webmvc.interceptor.LoginInterceptor" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta charset="utf-8">
    <title>会员账户管理</title>
    <link rel="shortcut icon" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="bookmark" href="${pageContext.request.contextPath}/favicon.ico"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/layui/css/layui.css" media="all">
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/layui/layui.js"
            charset="utf-8"></script>
    <script type="application/javascript" src="${pageContext.request.contextPath}/webjars/jquery/jquery.js"></script>
    <script type="application/javascript"
            src="${pageContext.request.contextPath}/private/js/loading/ajaxloading.js"></script>
</head>
<body>
<fieldset class="layui-elem-field">
    <legend>查询条件</legend>
    <form id="form1" class="layui-form" action="">
        <table width="96%" align="center">
            <tr height="23">
                <td align="center">课程顾问:</td>
                <td>
                    <select id="adminnid" name="adminnid" lay-verify="adminnid" lay-search>
                        <option value="">录入关键字查询并选择</option>
                    </select>
                </td>
                <td align="center">用户姓名:</td>
                <td>
                    <input id="username" name="username" class="layui-input" type="text" placeholder="请输用户姓名关键字"
                           autocomplete="off"
                           lay-verify="username">
                </td>
                <td align="center">有效状态:</td>
                <td>
                    <input id="validflag" name="validflag" type="checkbox" checked="" lay-skin="switch" value="1"
                           lay-text="有效|无效">
                </td>

            </tr>
            <tr height="23">
                <td align="center">账号创建开始时间:</td>
                <td>
                    <input id="starttime" name="starttime" class="layui-input" type="text" placeholder="yyyy-MM-dd"
                           autocomplete="off"
                           lay-verify="starttime">
                </td>
                <td align="center">账号创建结束时间:</td>
                <td>
                    <input id="endtime" name="endtime" class="layui-input" type="text" placeholder="yyyy-MM-dd"
                           autocomplete="off"
                           lay-verify="endtime">
                </td>
            </tr>
            <tr height="23">
                <td colspan="6" align="center">
                    <button class="layui-btn" lay-submit lay-filter="query">查询</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </td>
            </tr>
        </table>
    </form>
</fieldset>

<fieldset class="layui-elem-field">
    <legend>会员列表</legend>
    <table class="layui-table" lay-filter="gridfilter"
           lay-data="{url:'',page:true, id:'gridresult',height:'330px',width:'1080',toolbar:'#toolbarDemo',defaultToolbar: ['filter', 'print', 'exports'] } ">
        <thead>
        <tr>
            <th lay-data="{field:'userid',hide:true,width:'120'}">账号id</th>
            <th lay-data="{field:'openid',hide:true,align:'center',width:'120'}">openid</th>
            <th lay-data="{field:'username',align:'center',width:'120'}">用户姓名</th>
            <th lay-data="{field:'userstr',align:'center',width:'120'}">用户编号</th>
            <th lay-data="{field:'sexname',align:'center',width:'120'}">性别</th>
            <th lay-data="{field:'telephone',align:'center',width:'120'}" >联系电话</th>
            <th lay-data="{field:'validflagtext',align:'center',width:'100'}">状态</th>
            <th lay-data="{field:'tiyannums',align:'center',width:'120'}" >体验课时剩余数</th>
            <th lay-data="{field:'tiyansums',align:'center',width:'120'}" >体验课时总数</th>
            <th lay-data="{field:'restclass',align:'center',width:'100'}" >剩余课时</th>
            <th lay-data="{field:'totleclass',align:'center',width:'100'}">总课时</th>
            <th lay-data="{field:'totleamount',align:'center',width:'100'}">充值金额</th>
            <th lay-data="{field:'rechargenum',align:'center',width:'100'}">充值次数</th>
            <th lay-data="{field:'addtime',align:'center',width:'120'}">注册时间</th>
            <th lay-data="{field:'deadline',align:'center',width:'120'}">有效截止期</th>
            <th lay-data="{field:'addusername',align:'center',width:'120'}" >课程顾问</th>
            <th lay-data="{field:'updatetime',align:'center',width:'120'}" >修改时间</th>
            <th lay-data="{field:'updateusername',align:'center',width:'120'}">修改人</th>
            <th lay-data="{field:'usercomment',align:'center',width:'300'}">备注</th>
            <th lay-data="{fixed: 'right',width:'220',align:'center', toolbar: '#barDemo'}">操作</th>
        </tr>
        </thead>
    </table>
</fieldset>

<fieldset class="layui-elem-field">
    <legend>会员孩子列表</legend>
    <table class="layui-table" lay-filter="gridfilter_children"
           lay-data="{url:'',page:true, id:'gridresult_children',height:'330px',width:'1080',toolbar:'#toolbarDemo2',defaultToolbar: ['filter'] } ">
        <thead>
        <tr>
            <th lay-data="{field:'cid',hide:true,width:'120'}">孩子id</th>
            <th lay-data="{field:'userid',hide:true,width:'120'}">会员id</th>
            <th lay-data="{field:'cname',align:'center',width:'120'}">孩子姓名</th>
            <th lay-data="{field:'csex',align:'center',width:'120'}">性别</th>
            <th lay-data="{field:'birthday',align:'center',width:'150'}">出生日期</th>
            <th lay-data="{fixed: 'right',width:'220',align:'center', toolbar: '#barDemo2'}">操作</th>
        </tr>
        </thead>
    </table>
</fieldset>
</body>
<script id="barDemo" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{{ d.validflag=='1'?'置为无效' : '置为有效' }}</a>
    <% if(((AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME)).getAdminnid()==1) { %>
    <a class="layui-btn layui-btn-xs" lay-event="remove">删除记录</a>
    <% } %>
</script>
<script id="barDemo2" type="text/html">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <% if(((AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME)).getAdminnid()==1) { %>
    <a class="layui-btn layui-btn-xs" lay-event="remove">删除记录</a>
    <% } %>
</script>
<script id="toolbarDemo" type="text/html">
    <div class="layui-btn-container" style="display:none" >
        <button class="layui-btn layui-btn-sm"   lay-event="addRoleInfo">新增会员</button>
    </div>
</script>
<script id="toolbarDemo2" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm"   lay-event="addChildrenInfo">新增孩子</button>
    </div>
</script>
<script type="application/javascript">
    var form;
    layui.use(['form', 'table','laydate'], function () {
        form = layui.form;
        //日期控件
        var laydate = layui.laydate;
        //执行一个laydate实例
        laydate.render({
            elem: '#starttime' //指定元素
        });
        laydate.render({
            elem: '#endtime' //指定元素
        });
        //switch off状态赋值
        form.on('switch', function (data) {
            $(data.elem).attr('type', 'hidden').val(this.checked ? '1' : '0');
        });

        //数据表格模块
        var table = layui.table;
        //初始化时加载会员列表
        table.reload('gridresult', {
            url: '${pageContext.request.contextPath}/eyas/userinfo/queryUserInfo'
            , where: {validflag:'1'} //设定异步数据接口的额外参数
            , page: {
                curr: 1
            },
            method:'get'
        });
        //监听提交
        form.on('submit(query)', function (data) {
            table.reload('gridresult', {
                url: '${pageContext.request.contextPath}/eyas/userinfo/queryUserInfo'
                , where: data.field //设定异步数据接口的额外参数
                , page: {
                    curr: 1
                },
                method:'get'
            });
            table.reload('gridresult_children', {
                url: ''
                , where: {} //设定异步数据接口的额外参数
                , page: {
                    curr: 1
                },
                method:'get'
            });
            selected_userid=null;
            return false;
        });
        //监听行工具事件
        table.on('tool(gridfilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                $.ajax({
                    type: "POST",
                    url: "${pageContext.request.contextPath}/eyas/userinfo/changeValidflag",
                    data:{userid:data.userid},
                    dataType: "json",
                    success: function (data) {
                        layer.msg("修改成功");
                        table.reload('gridresult_children', {
                            url: ''
                            , where: {} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                        selected_userid=null;
                        obj.del();
                    },
                    error: function (xdata) {
                        layer.msg(xdata.responseJSON.errormsg);
                    }
                });
            } else if (obj.event === 'edit') {
                var win_1 = layer.open({
                    type: 2,
                    title: '会员账户编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/userinfo/edit?userid='+data.userid,
                    end:function() {
                        var loginname = $("#loginname").val();
                        var username = $("#username").val();
                        var validflag = $("#validflag").val();
                        var starttime = $("#starttime").val();
                        var endtime = $("#endtime").val();
                        table.reload('gridresult', {
                            url: '${pageContext.request.contextPath}/eyas/userinfo/queryUserInfo'
                            , where: {loginname:loginname,username:username,validflag:validflag,starttime:starttime,endtime:endtime} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });

                        table.reload('gridresult_children', {
                            url: ''
                            , where: {} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                        selected_userid=null;
                    }
                });
                layer.full(win_1);
            } else if(obj.event === 'remove') {
                if(data.validflag!='0') {
                    layer.msg("只能删除[无效]状态的数据");
                    return;
                }
                $.ajax({
                    type : "DELETE",
                    url : "${pageContext.request.contextPath}/eyas/admin/superadmin/userinfo/userid/"+data.userid,
                    dataType : "json",
                    success: function(data) {
                        layer.msg("删除成功");
                        var loginname = $("#loginname").val();
                        var username = $("#username").val();
                        var validflag = $("#validflag").val();
                        var starttime = $("#starttime").val();
                        var endtime = $("#endtime").val();
                        table.reload('gridresult', {
                            url: '${pageContext.request.contextPath}/eyas/userinfo/queryUserInfo'
                            , where: {loginname:loginname,username:username,validflag:validflag,starttime:starttime,endtime:endtime} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });

                        table.reload('gridresult_children', {
                            url: ''
                            , where: {} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                        selected_userid=null;
                    },
                    error:function(xdata){
                        layer.msg(xdata.responseJSON.errormsg);
                    }
                });
            }
        });
        //头工具栏事件
        table.on('toolbar(gridfilter)', function(obj){
            if (obj.event === 'addRoleInfo') {
                var win_1 = layer.open({
                    type: 2,
                    title: '会员账户编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/userinfo/edit',
                    end:function() {
                        var openid = $("#openid").val();
                        var username = $("#username").val();
                        var validflag = $("#validflag").val();
                        var starttime = $("#starttime").val();
                        var endtime = $("#endtime").val();
                        table.reload('gridresult', {
                            url: '${pageContext.request.contextPath}/eyas/userinfo/queryUserInfo'
                            , where: {openid:openid,username:username,validflag:validflag,starttime:starttime,endtime:endtime} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });

                        table.reload('gridresult_children', {
                            url: ''
                            , where: {} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                        selected_userid=null;
                    }
                });
                layer.full(win_1);
            }
        });
        //加载顾问下拉框
        selectAdminInfo();

        var selected_userid;
        //监听行单击事件
        table.on('row(gridfilter)', function(obj){
            var data = obj.data;
            selected_userid = data.userid;
            //标注选中样式
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            table.reload('gridresult_children', {
                url: '${pageContext.request.contextPath}/eyas/userinfo/queryChildrenInfoByUser'
                , where: {userid:selected_userid} //设定异步数据接口的额外参数
                , page: {
                    curr: 1
                },
                method:'get'
            });
        });


        //监听行工具事件
        table.on('tool(gridfilter_children)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                var win_1 = layer.open({
                    type: 2,
                    title: '会员孩子编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/userinfo/editChildren?cid='+data.cid,
                    end:function() {
                        table.reload('gridresult_children', {
                            url: '${pageContext.request.contextPath}/eyas/userinfo/queryChildrenInfoByUser'
                            , where: {userid:selected_userid} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                    }
                });
                layer.full(win_1);
            }else if(obj.event === 'remove') {
                $.ajax({
                    type : "DELETE",
                    url : "${pageContext.request.contextPath}/eyas/admin/superadmin/childreninfo/cid/"+data.cid,
                    dataType : "json",
                    success: function(data) {
                        layer.msg("删除成功");
                        table.reload('gridresult_children', {
                            url: '${pageContext.request.contextPath}/eyas/userinfo/queryChildrenInfoByUser'
                            , where: {userid:selected_userid} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                    },
                    error:function(xdata){
                        layer.msg(xdata.responseJSON.errormsg);
                    }
                });
            }
        });

        //头工具栏事件
        table.on('toolbar(gridfilter_children)', function(obj){
            if (obj.event === 'addChildrenInfo') {
                if(!selected_userid) {
                    layer.msg("请单击选择一条会员信息");
                    return;
                }
                var win_1 = layer.open({
                    type: 2,
                    title: '会员孩子编辑',
                    shadeClose: true,
                    shade: false,
                    maxmin: true, //开启最大化最小化按钮
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/eyas/userinfo/editChildren?userid='+selected_userid,
                    end:function() {
                        table.reload('gridresult_children', {
                            url: '${pageContext.request.contextPath}/eyas/userinfo/queryChildrenInfoByUser'
                            , where: {userid:selected_userid} //设定异步数据接口的额外参数
                            , page: {
                                curr: 1
                            },
                            method:'get'
                        });
                    }
                });
                layer.full(win_1);
            }
        });

    });


    function selectAdminInfo() {
        $.ajax({
            url:"${pageContext.request.contextPath}/eyas/admininfo/queryAdminInfoList",
            type:"GET",
            dataType:"json",
            success:function(result){
                var list = result;    //返回的数据
                var admin = document.getElementById("adminnid");        //add_role_name给select定义的id
                for(var i=0;i<list.length;i++){
                    var option = document.createElement("option");    // 创建添加option属性
                    option.setAttribute("value",list[i].adminnid);                  // 给option的value添加值
                    option.innerText=list[i].username;             // 打印option对应的纯文本 （超级管理员、管理员）
                    admin.appendChild(option);                          // 给select 添加option子标签
                }

                form.render("select");                                // 刷性select，显示出数据
            },
            error:function(xdata){
                layer.msg(xdata.responseJSON.errormsg);
            }
        });
    }
</script>
</html>
